﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using drinking.Managers;
using drinking.Models;
using drinking.Helpers;
using Newtonsoft.Json;

namespace drinking.Api.WX
{
    /// <summary>
    /// 微信小程序端api
    /// </summary>
    [Route("api/wx")]
    [ApiController]
    public class WxController : ControllerBase
    {



        /// <summary>
        /// 微信登录,登录成功则返回用户信息
        /// </summary>
        /// <param name="code">用来获取手机号的code</param>
        /// <returns></returns>
        [HttpGet("login")]
        public ApiResult WxLogin(string code)
        {
            // 登录的时候优先使用token匹配， token无效再通过opneid匹配
            string token = HttpContext.Request.Headers["token"];
            Customer customer;
            if (string.IsNullOrEmpty(token)) // 请求头无token
            {
                customer = UserManager.InitCustomerByOpenid(code);
            }
            else // 请求头有token
            {
                // token 有效
                if (UserManager.CacheCustomers.TryGetValue(token, out customer))
                {
                    UserManager.CacheCustomers[token].LastAccessTime = DateTime.Now;
                }
                else // token无效效 , 根据opneid 匹配
                {
                    customer = UserManager.InitCustomerByOpenid(code);
                }
            }
            return ApiResult.Success(customer);
        }


        /// <summary>
        /// 微信绑定手机号
        /// </summary>
        /// <param name="phone_code">用来获取手机号的code</param>
        /// <returns></returns>
        [HttpGet("bindPhone")]
        public ApiResult BindPhone(string phone_code)
        {
            string phone = WxManager.GetUserPhoneNumber(phone_code);
            if (string.IsNullOrEmpty(phone))
            {
                throw new Exception("phone code失效");
            }
            Customer c = UserManager.CurrCustomer.Value;
            new DBContext().Db.Updateable(c).UpdateColumns(c => new { c.Phone }).ExecuteCommand();
            c.Phone = phone;
            return ApiResult.Success(c);
        }


        /// <summary>
        /// 获取门店列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("storeList")]
        public ApiResult StoreList()
        {
            // 返回Store到前段，注意需要屏蔽掉 密码字段
            List<Store> list = new DBContext().Db.Queryable<Store>().IgnoreColumns(it => new { it.Password, it.Token }).ToList();
            return ApiResult.Success(list);
        }

        /// <summary>
        /// 获取主页饮品 列表， 不分页
        /// </summary>  
        /// <param name="storeId">门店id</param>
        /// <returns></returns>
        [HttpGet("drinkList")]
        public ApiResult DrinkList(int storeId)
        {

            int index = StoreManager.GetStoresIndex4Id(storeId);
            if (index == -1)
            {
                throw new Exception("storeId 不存在");
            }

            // 将缓存中的数据进行深拷贝
            Store store = JsonConvert.DeserializeObject<Store>(JsonConvert.SerializeObject(StoreManager.CacheStores[index]));
            // 脱敏
            store.Password = "";
            return ApiResult.Success(store);
        }


        /// <summary>
        /// 返回一些全局的参数信息
        /// </summary>
        /// <param name="phone_code">用来获取手机号的code</param>
        /// <returns></returns>
        [HttpGet("globalParams")]
        public ApiResult GlobalParams()
        {
            return ApiResult.Success(new
            {
                AccessoryPrices = new List<decimal>() { 1, 1, 1, 1 },

            }); 
        }

    }
}
